package com.niit.musicserve.controller;

import com.alibaba.fastjson.JSONObject;
import com.niit.musicserve.annotation.RequireRole;
import com.niit.musicserve.entity.RankSong;
import com.niit.musicserve.service.RankSongService;
import com.niit.musicserve.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/rankSong")
public class RankSongController {
	
	@Autowired
	private RankSongService rankSongService;
	
	/**
	 * 添加榜单歌曲
	 * @param rankSong 前端数据
	 * @return 响应结果
	 */
	@RequestMapping("/insertRankSong")
	@RequireRole("admin")
	public Result<String> insertRankSong(@RequestBody RankSong rankSong) {
		
		try {
			
			if (rankSong.getSongId() == null || rankSong.getRankId() == null ){
				return Result.error("入榜歌曲不能为空");
			}
			
			rankSong.setSongId(rankSong.getSongId());
			rankSong.setRankId(rankSong.getRankId());
			
			boolean flag = rankSongService.insertRankSong(rankSong);
			
			if(flag) {
				
				return Result.success("该榜单添加歌曲成功",null);
			}else {
				
				return Result.error("该榜单添加歌曲失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("添加失败: " + e.getMessage());
		}
		
		
	}
	
	/**
	 * 删除榜单歌曲
	 * @param request 前端数据
	 * @return 响应结果
	 */
	@RequestMapping("/deleteRankSong")
	@RequireRole("admin")
	public Result<String> deleteRankSong(HttpServletRequest  request) {
	
	String rankSongId = request.getParameter("rankSongId").trim();
	
	boolean flag = rankSongService.deleteRankSong(Integer.parseInt(rankSongId));
	
	if ( flag){
		return Result.success("该榜单删除歌曲成功",null);
	}
		return Result.error("该榜单删除歌曲失败");
	}
	
	/**
	 * 根据id删除榜单歌曲
	 * @param request 前端数据
	 * @return 响应结果
	 */
	@RequestMapping("/deleteRankSongByRankIdAndRankSongId")
	@RequireRole("admin")
	public Result<String> deleteRankSongByRankIdAndRankSongId(HttpServletRequest  request) {

		String rankId = request.getParameter("rankId").trim();
		String songId = request.getParameter("songId").trim();
		
		boolean flag = rankSongService.deleteRankSongByRankIdAndSongId(Integer.parseInt(rankId),Integer.parseInt(songId));
		if ( flag){
			return Result.success("该榜单删除歌曲成功",null);
		}
		return Result.error("该榜单删除歌曲失败");
	}
	
	/**
	 * 修改榜单歌曲
	 * @param rankSong 榜单歌曲
	 * @return 响应结果
	 */
	@RequestMapping("/updateRankSong")
	@RequireRole("admin")
	public Result<String> updateRankSong(RankSong rankSong) {
		
		if(rankSongService.updateRankSong(rankSong)) {
			
			return Result.success("修改成功",null);
		}else {
			
			return Result.error("修改失败");
		}
	}
	
	/**
	 * 根据榜单id查询榜单内的歌曲
	 * @param request 前端数据
	 * @return 响应结果
	 */
	@RequestMapping("/rankSongByRankId")
	@RequireRole("admin")
	public Result<JSONObject> rankSongByRankId(HttpServletRequest request) {
		try {
			String rankId = request.getParameter("rankId").trim();
			
			List<RankSong> rankSongs = rankSongService.rankSongByRankId(Integer.parseInt(rankId));
			
			// 构造返回结果
			JSONObject result = new JSONObject();
			result.put("list", rankSongs);
			result.put("total", rankSongs.size());
			
			return Result.success("查询成功", result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("查询失败: " + e.getMessage());
		}
	}
	
}
